import Vue from 'vue'
import Router from 'vue-router'
import routes from './routes'
import iview from 'iview'
import {Auth} from '../utils/auth'
import isMobile from '../utils/systemInfo'

console.log('isMobile:', isMobile())
/*
* 1.登录状态控制
* 2.路由级别 权限控制（暂时未做）
* */
Vue.use(Router);

iview.LoadingBar.config({
  height: 5
});
// const AuthStatus = Auth({});// 登录状态

const router = new Router({
  routes
});
/*
* 1.通过登录信息来判断 页面跳转的形式
* 2.细节比较多、留意
* */
router.beforeEach((to, from, next) => {
  let meta = to.meta || {};
  iview.LoadingBar.start();
  if (meta.requiredAuth) {
    // Auth({}) && next(); // 登录
    if (!Auth({}) && (to.name !== 'Login')) {
      next({
        name: 'Login'
      })
    } else if (Auth({}) && (to.name === 'Login')) {
      !isMobile()['isMobile'] && next({name: 'OrderConfig'});
      isMobile()['isMobile'] && next({name: 'MobileGraphDetail'})
    } else if (Auth({}) && (to.name === 'OrderConfig')) {
      !isMobile()['isMobile'] && next();
      isMobile()['isMobile'] && next({name: 'MobileGraphDetail'})
    } else if (Auth({}) && (to.name === 'MobileGraphDetail')) {
      !isMobile()['isMobile'] && next({name: 'OrderConfig'});
      isMobile()['isMobile'] && next()
    } else {
      next()
    }
  } else {
    next()
  }
});
router.afterEach((to) => {
  document.title = to.meta ? to.meta.title : '影子官网编辑后台';
  iview.LoadingBar.finish();
});

export default router
